package action;

import java.util.ArrayList;
import java.util.Map;


import models.IssueData;
import models.TagData;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SearchIssueAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private String byCategory="";
	private String byTag="";
	private int byPriority;
	private String byStatus;
	private String selectedTagName="";

	public String getSelectedTagName() {
		return selectedTagName;
	}

	public void setSelectedTagId(String selectedTagName) {
		this.selectedTagName = selectedTagName;
	}

	public ArrayList<IssueData> getIssueList() {
		return IssueList;
	}

	public void setIssueList(ArrayList<IssueData> issueList) {
		IssueList = issueList;
	}

	public ArrayList<models.TagData> getTagList() {
		return TagList;
	}

	public void setTagList(ArrayList<models.TagData> tagList) {
		TagList = tagList;
	}



	private int selected_issue_id;
	public String getByStatus() {
		return byStatus;
	}

	public void setByStatus(String byStatus) {
		this.byStatus = byStatus;
	}



	private String searchIssues = "";
	Map<String, Object> session;
	ArrayList<IssueData> IssueList= new ArrayList<IssueData>();
	ArrayList<IssueData> IssueListTemp1= new ArrayList<IssueData>();
	ArrayList<IssueData> IssueListTemp2= new ArrayList<IssueData>();
	ArrayList<models.TagData> TagList= new ArrayList<models.TagData>();
	ArrayList<models.TagData> Tags= new ArrayList<models.TagData>();
	ArrayList<models.DeveloperData> DeveloperList=new ArrayList<models.DeveloperData>();
	
	public ArrayList<models.DeveloperData> getDeveloperList() {
		return DeveloperList;
	}

	public void setDeveloperList(ArrayList<models.DeveloperData> developerList) {
		DeveloperList = developerList;
	}

	public void setSelectedTagName(String selectedTagName) {
		this.selectedTagName = selectedTagName;
	}

	public String getSearchIssues() {
		return searchIssues;
	}

	public void setSearchIssues(String searchIssues) {
		this.searchIssues = searchIssues;
	}


	
	public String getByCategory() {
		return byCategory;
	}

	public void setByCategory(String byCategory) {
		this.byCategory = byCategory;
	}

	public String getByTag() {
		return byTag;
	}

	public void setByTag(String byTag) {
		this.byTag = byTag;
	}

	public int getByPriority() {
		return byPriority;
	}

	public void setByPriority(int byPriority) {
		this.byPriority = byPriority;
	}
	public String execute() {

		System.out.println("in Login.execute() with buttonvalue = " + 
	    	       this.searchIssues);
				
			if(this.searchIssues.equalsIgnoreCase("Search !!")){
				
				//System.out.println("in lodge");
				if(this.byStatus.equals("-1")){
				if(!(this.byCategory.equals("-1") || this.byPriority== -1)){
					IssueListTemp1 = IssueData.find("where issue_category = '"+ this.byCategory +"' and issue_priority = "+ this.byPriority );
					System.out.println("in if 11"+this.byCategory);
					
				}
				else if(!this.byCategory.equals("-1")){
					IssueListTemp1 = IssueData.find("where issue_category = '"+ this.byCategory +"'");
					System.out.println("in else if 12 "+this.byCategory);

				}
				else if(this.byPriority!= -1){
					IssueListTemp1 = IssueData.find("where issue_priority = "+ this.byPriority);
					System.out.println("in else if 13 "+this.byPriority);

				}
				else{
					IssueListTemp1 = IssueData.find("");
					System.out.println("in else 14");

				}
				}
				else{
						if(!(this.byCategory.equals("-1") || this.byPriority== -1)){
							IssueListTemp1 = IssueData.find("where issue_category = '"+ this.byCategory +"' and issue_status = '"+ this.byStatus + "'and issue_priority = "+ this.byPriority );
							System.out.println("in if 21"+this.byCategory);
							
						}
						else if(!this.byCategory.equals("-1")){
							IssueListTemp1 = IssueData.find("where issue_category = '"+ this.byCategory +"' and issue_status = '"+ this.byStatus + "'");
							System.out.println("in else if 22 "+this.byCategory);

						}
						else if(this.byPriority!= -1){
							IssueListTemp1 = IssueData.find("where issue_priority = "+ this.byPriority +" and issue_status = '"+ this.byStatus + "'");
							System.out.println("in else if 23 "+this.byPriority);

						}
						else{
							IssueListTemp1 = IssueData.find("where issue_status = '"+ this.byStatus + "'");
							System.out.println("in else 24");

						}
					
				}
				System.out.println("--------------------------- "+this.getSelectedTagName()+"-------------------------");
				if(this.getSelectedTagName().equals("-1")){
					for(int i=0;i<IssueListTemp1.size();i++){	
						IssueList.add(IssueListTemp1.get(i));
					}
					System.out.println("in if 31"+this.byTag);

				}
				else{
					Tags = models.TagData.Issue_id_unique("where tag_name = '"+ this.getSelectedTagName() +"'" );
					System.out.println("in else 32");
					//System.out.println("--------------"+Tags.size()+"---------------");
					
					for(int i=0;i<Tags.size();i++){
						System.out.println("--------------"+Tags.get(i).getIssueId()+"---------------");
						for(int j=0;j<IssueListTemp1.size();j++){
							
							if(Tags.get(i).getIssueId()==IssueListTemp1.get(j).getIssue_id()){
								IssueList.add(IssueListTemp1.get(j));
							}
						}
					}
					
				}
				TagList = models.TagData.tag_name_unique("");
				session = ActionContext.getContext().getSession();
				session.put("IssueList",IssueList);
				session.put("TagList",TagList);
				DeveloperList=models.DeveloperData.find("");
				session.put("DeveloperList",DeveloperList);
				
				
			
				
			}

			return "issues";

	}   

	public int getSelected_issue_id() {
		return selected_issue_id;
	}

	public void setSelected_issue_id(int selected_issue_id) {
		this.selected_issue_id = selected_issue_id;
	}

}
